import {
  Card,
  Breadcrumb,
  Form,
  Radio,
  DatePicker,
  Button,
  Table,
  Tag,
  Space,
  Modal,
} from 'antd'
import { Link } from 'react-router-dom'
import 'moment/locale/zh-cn'
import { useEffect, useRef } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { getList, delArticle } from '@/store/actions/article'
import styles from './index.module.scss'
import noPicture from '@/assets/images/error.png'
import {
  EditOutlined,
  DeleteOutlined,
  ExclamationCircleOutlined,
} from '@ant-design/icons'
import { history } from '@/utils/history'
import Channel from '@/components/Channel'

const { RangePicker } = DatePicker
const Article = () => {
  const dispatch = useDispatch()

  useEffect(() => {
    dispatch(getList({}))
  }, [])

  //文章状态
  const articleStatus = {
    0: { color: 'yellow', status: '草稿' },
    1: { color: '#ccc', status: '待审核' },
    2: { color: 'green', status: '审核通过' },
    3: { color: 'red', status: '审核失败' },
  }

  //delete
  const delItem = (text) => {
    // console.log(text)
    Modal.confirm({
      title: '温馨提示',
      icon: <ExclamationCircleOutlined />,
      content: '此操作将永久删除该文章, 是否继续？',
      onOk: async () => {
        await dispatch(delArticle(text.id))
        await dispatch(getList(paramsRef.current))
      },
    })
  }

  const columns = [
    {
      title: '封面',
      dataIndex: 'cover',

      render: function (text) {
        return text.type ? (
          <img src={text.images[0]} width={200} height={150} alt="" />
        ) : (
          <img src={noPicture} width={200} height={150} alt="" />
        )
      },
    },
    {
      title: '标题',
      dataIndex: 'title',
    },
    {
      title: '状态',
      dataIndex: 'status',
      render: function (text) {
        const { color, status } = articleStatus[text]
        return <Tag color={color}>{status}</Tag>
      },
    },
    {
      title: '发布时间',
      dataIndex: 'pubdate',
    },
    {
      title: '阅读数',
      dataIndex: 'read_count',
    },
    {
      title: '评论数',
      dataIndex: 'comment_count',
    },
    {
      title: '点赞数',
      dataIndex: 'like_count',
    },
    {
      title: '操作',
      // dataIndex: 'address',
      render: (text) => {
        return (
          <Space size={'middle'}>
            <Button
              type="primary"
              shape="circle"
              icon={<EditOutlined />}
              onClick={() => {
                history.push('/home/publish/' + text.id)
              }}
            />
            <Button
              type="primary"
              danger
              shape="circle"
              icon={<DeleteOutlined />}
              onClick={() => delItem(text)}
            />
          </Space>
        )
      },
    },
  ]
  const {
    list: data,
    total_count,
    page,
    per_page,
  } = useSelector((state) => state.article)

  //0-草稿，1-待审核，2-审核通过，3-审核失败，不传为全部

  let paramsRef = useRef({})
  //筛选功能
  const onFinish = (values) => {
    console.log(values)
    const { channel_id, date, status } = values
    const params = {}
    if (status !== -1) {
      params.status = status
    }
    if (channel_id !== undefined) {
      params.channel_id = channel_id
    }
    if (date) {
      params.begin_pubdate = date[0].format('YYYY-MM-DD')
      params.end_pubdate = date[1].format('YYYY-MM-DD')
    }
    dispatch(getList(params))
    paramsRef.current = params
  }

  const onChangePage = (page, pageSize) => {
    const data = {
      ...paramsRef.current,
      page,
      per_page: pageSize,
    }
    dispatch(getList(data))
    paramsRef.current = data
  }

  return (
    <div className={styles.root}>
      {/*<div className="site-card-border-less-wrapper">*/}
      <Card
        title={
          <Breadcrumb separator=">">
            <Breadcrumb.Item>
              <Link to="/home">首页</Link>
            </Breadcrumb.Item>
            <Breadcrumb.Item>
              <a href="">内容管理</a>
            </Breadcrumb.Item>
          </Breadcrumb>
        }
        bordered={false}
        style={{
          width: '100%',
          marginBottom: 20,
        }}
      >
        <Form initialValues={{ status: -1 }} onFinish={onFinish}>
          <Form.Item name="status" label="状态">
            <Radio.Group>
              <Radio value={-1}>全部</Radio>
              <Radio value={0}>草稿</Radio>
              <Radio value={1}>待审核</Radio>
              <Radio value={2}>审核通过</Radio>
              <Radio value={3}>审核失败</Radio>
            </Radio.Group>
          </Form.Item>
          <Form.Item label="频道" name="channel_id">
            <Channel
              style={{
                width: 300,
              }}
            ></Channel>
          </Form.Item>
          <Form.Item label="日期" name="date">
            <RangePicker
              style={{
                width: 300,
              }}
            />
          </Form.Item>
          <Form.Item>
            <Button type="primary" htmlType="submit">
              筛选
            </Button>
          </Form.Item>
        </Form>
      </Card>

      <Card title={`根据筛选条件共查询到 ${total_count} 条结果：`}>
        <Table
          columns={columns}
          dataSource={data}
          rowKey="id"
          pagination={{
            current: page,
            total: total_count,
            position: ['bottomCenter'],
            onChange: onChangePage,
          }}
        />
      </Card>
      {/*</div>*/}
    </div>
  )
}
export default Article
